package org.dwpd.controller.message;

import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dwpd.domain.common.Result;
import org.dwpd.domain.message.Message;
import org.dwpd.service.message.MessageService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RequestMapping("/message")
@RestController
@RequiredArgsConstructor
public class MessageController {

//    @Resource
    private final MessageService messageService;

    /**
     * 添加新的用户帖
     * @param message 当前用户传进来的用户帖数据
     */
    @PostMapping("/addMessage")
    public Result addMessage(@RequestBody Message message, HttpServletRequest request) {
        return messageService.addMessage(message, request);
    }

    /**
     * 根据用户id获取用户所有已发贴
     */
    @GetMapping("/getAllMessageById")
    public Result getAllMessageById() {
        return messageService.getAllMessageById();
    }


    /**
     * 用户申请上座
     */
    @PostMapping("/addSeat")
    public Result addSeat(Integer messageId) {
        return messageService.addSeat(messageId);
    }

    /**
     * 同意用户上座请求
     */
    @PostMapping("/agreeUserSeat")
    public Result agreeUserSeat(Integer userId, Integer messageId) {
        return messageService.agreeUserSeat(userId, messageId);
    }

    /**
     * 拒绝用户上座请求
     */
    @PostMapping("/rejectUserSeat")
    public Result rejectUserSeat(Integer userId, Integer messageId) {
        return messageService.rejectUserSeat(userId, messageId);
    }

    /**
     * 将用户从座位上移除
     */
    @PostMapping("/removeUserSeat")
    public Result removeUserSeat(Integer userId, Integer messageId) {
        return messageService.removeUserSeat(userId, messageId);
    }

    /**
     * 用户主动离座
     */
    @PostMapping("/userLeaveSeat")
    public Result userLeaveSeat(Integer messageId) {
        return messageService.userLeaveSeat(messageId);
    }

    /**
     * 在首页获取当前用户帖的详细信息
     */
    @GetMapping("/getMessageDetailById")
    public Result getMessageDetailById(Integer messageId) {
        return messageService.getMessageDetailById(messageId);
    }

    /**
     * 在发布页面获取用户帖详情信息
     * @param messageId 帖子ID
     */
    @GetMapping("/getMessageDetailByUser")
    public Result getMessageDetailByUser(@RequestParam("messageId") Integer messageId) {
        return messageService.getMessageDetailByUser(messageId);
    }

    /**
     * 拼车任务完成后结束用户帖(完成非删除)
     */
    @PostMapping("/accomplishMessage")
    public Result accomplishMessage(Integer messageId) {
        return messageService.updateAccomplish(messageId);
    }

    /**
     * 删除用户帖
     */
    @PostMapping("/deleteMessage")
    public Result deleteMessage(Integer messageId) {
        return messageService.deleteMessage(messageId);
    }

    /**
     * 修改用户帖信息
     */
    @PostMapping("/updateMessage")
    public Result updateMessage(@RequestBody Message message) {
        return messageService.updateMessage(message);
    }

    /**
     * 当前系统时间超过用户贴出发时间结束帖子
     */
    @PostMapping("/updateMessageOverTime")
    public Result updateMessageOverTime() {
        return messageService.updateMessageOverTime();
    }

    /**
     * 根据分类获取所有用户帖
     */
    @GetMapping("/getAllMessage")
    public Result getAllMessage(Integer typeId) {
        return messageService.getAllMessage(typeId);
    }

    /**
     * 用户锁帖
     * @param messageId 帖子ID
     */
    @Transactional
    @PostMapping("/lockMessage")
    public Result lockMessage(Integer messageId) {
        return messageService.lockMessage(messageId);
    }

    /**
     * 获取指定帖子上座用户id信息
     * @param messageId 帖子ID
     */
    @GetMapping("/getOnSeatUsersByMessageId")
    public Result getOnSeatUsersByMessageId(Integer messageId) {
        return messageService.getOnSeatUsersByMessageId(messageId);
    }

    /**
     * 判断当前用户是否为帖主
     */
    @GetMapping("/getUserRootFlagInMessage")
    public Result getUserRootFlagInMessage(Integer messageId) {
        return messageService.getUserRootFlagInMessage(messageId);
    }

    /**
     * 获取指定用户的历史拼车(上座，不包括自发帖)信息
     */
    @GetMapping("/getOnSeatHistoryByUserId")
    public Result getOnSeatHistoryByUserId(Integer userId) {
        return messageService.getOnSeatHistoryByUserId(userId);
    }

}
